#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "../../../mac/mac.h"

static struct timeval timer;

#if 0

typedef void (*timer_callback)();

struct mtimer_t_ {
    /*
    timer_call_back func;
    uint32 expire;
    uint8 index;
    struct mtimer_t w_timer;
    */
} mtimer_t;

int8 timer_init(mtimer_t *timer, timer_callback func);
int8 timer_start(mtimer_t *timer, uint16 expire);
int8 timer_stop(timre_t *timer);

int32 timer_get();

#endif


int8 timer_init(mtimer_t *timer, timer_callback func)
{
    timer->func = func;
    return MAC_OK;
}

int32 timer_get(void)
{
    gettimeofday(&timer, NULL);
    return (uint32)timer.tv_usec;
}

int8 timer_start(mtimer_t *timer, uint16 expire)
{
    struct sigaction sa;
    struct itimerval itimer;

    memset (&sa, 0, sizeof (sa));
    sa.sa_handler = (void(*)(int))timer->func;
    sigaction (SIGALRM, &sa, NULL);
    itimer.it_value.tv_sec = 0;
    itimer.it_value.tv_usec = timer->expire;
    itimer.it_interval.tv_sec = 0;
    itimer.it_interval.tv_usec = 0;
    setitimer (ITIMER_REAL, &itimer, NULL);

    return MAC_OK;
}

int8 timer_stop(mtimer_t *timer)
{
}

